package com.pc.cloud.core;

import com.pc.cloud.utils.IndexHomeUtils;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LuceneSettings
{
	private static final Logger log = Logger.getLogger(LuceneSettings.class);
	private String searchType;
	private Analyzer analyzer;
	private Directory directory;
	private String home;
	private int ramNumDocs;
	private int mergeFactor;
	private int maxMergeDocs;
	private int maxBufferedDocs;
	private double ramBufferSizeMB;
	private boolean isOnRam = true;
	
	
	public boolean isOnRam() {
		return isOnRam;
	}

	public void setOnRam(boolean isOnRam) {
		this.isOnRam = isOnRam;
	}

	public int getRamNumDocs() {
		return ramNumDocs;
	}

	public void setRamNumDocs(int ramNumDocs) {
		this.ramNumDocs = ramNumDocs;
	}

	public LuceneSettings(String searchType){
		super();
		this.searchType = searchType;		
		home = IndexHomeUtils.getIndexerDirectory();
	}
	
	public LuceneSettings(Analyzer analyzer, String searchType)
	{
		super();
		this.analyzer = analyzer;
		this.searchType = searchType;
	}
	
	public void init() throws Exception {
		useFSDirectory();
	}
	
	public void useRAMDirectory() throws Exception {
		this.directory = new RAMDirectory();
		IndexWriter writer = new IndexWriter(this.directory, this.analyzer, true,IndexWriter.MaxFieldLength.LIMITED);
		writer.close();
	}
	
	public void useFSDirectory() throws Exception {				
		assert(this.getSearchType() != null && !"".equals(this.getSearchType()));		
		File file = new File(home + this.getSearchType());
		this.directory = FSDirectory.open(file);
		
		if (!IndexReader.indexExists(this.directory)) {
			this.createIndexDirectory();
		} 		
	}
		
	public void createIndexDirectory() throws Exception {
		assert(this.getSearchType() != null && !"".equals(this.getSearchType()));
        File file = new File(home + this.getSearchType());
		FSDirectory fsDir = FSDirectory.open(file);
		IndexWriter writer = new IndexWriter(fsDir, this.analyzer, true,IndexWriter.MaxFieldLength.LIMITED);
		writer.close();
	}	
	
	public Directory directory() {
		return this.directory;
	}
	
	public Analyzer analyzer(){
		return this.analyzer;
	}
	
	public static String formatDateTime(Date date) {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
	}

	public Analyzer getAnalyzer() {
		return analyzer;
	}

	public void setAnalyzer(Analyzer analyzer) {
		this.analyzer = analyzer;
	}

	public Directory getDirectory() {
		return directory;
	}

	public void setDirectory(Directory directory) {
		this.directory = directory;
	}

	public String getIndexWritePath() {
		return home;
	}

	public void setIndexWritePath(String indexWritePath) {
		this.home = indexWritePath;
	}

	public int getMergeFactor() {
		return mergeFactor;
	}

	public void setMergeFactor(int mergeFactor) {
		this.mergeFactor = mergeFactor;
	}

	public int getMaxMergeDocs() {
		return maxMergeDocs;
	}

	public void setMaxMergeDocs(int maxMergeDocs) {
		this.maxMergeDocs = maxMergeDocs;
	}

	public int getMaxBufferedDocs() {
		return maxBufferedDocs;
	}

	public void setMaxBufferedDocs(int maxBufferedDocs) {
		this.maxBufferedDocs = maxBufferedDocs;
	}

	public double getRamBufferSizeMB() {
		return ramBufferSizeMB;
	}

	public void setRamBufferSizeMB(double ramBufferSizeMB) {
		this.ramBufferSizeMB = ramBufferSizeMB;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public String getHome() {
		return home;
	}
}
